<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>有效的正方形</title>
</head>

<body>
  <script>
    function val(p1, p2, p3, p4) {
      let [A, B, D, C] = [p1, p2, p3, p4].sort((a, b) => {
        if (a[1] == b[1]) {
          return a[0] - b[0]
        } else {
          return b[1] - a[1]
        }
      });
      // 因不用求出边长具体值，所以不必开方
      const calDistance = (A, B) => {
        return Math.pow(A[0] - B[0], 2) + Math.pow(A[1] - B[1], 2);
      }

      let a = calDistance(A, B)
      let b = calDistance(B, C)
      let c = calDistance(C, D)
      let d = calDistance(D, A)
      let l1 = calDistance(A, C)
      let l2 = calDistance(B, D)

      return a == b && b == c && c == d && d == a && a != 0 && l1 == l2;
    };
    console.log(val(p1 = [0, 0], p2 = [1, 1], p3 = [1, 0], p4 = [0, 1]));
  </script>
</body>

</html>